package cn.edu.ujn.lab3.web;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.Products;
import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.BaseDictService;
import cn.edu.ujn.lab3.service.ProductsService;
import cn.edu.ujn.lab3.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/products")
public class ProductsController {

    private final String CATEGORIES_TYPE = "010";

    @Autowired
    private ProductsService productsService;

    @Autowired
    private BaseDictService baseDictService;
    //产品列表查询
    @RequestMapping(value = "/list.action")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer rows,
                       String prodName, String prodPrice,
                       String prodCategories, String prodState,
                       String prodRemark, Model model) {
        Page<Products> productsList = productsService.findProductsList(page, rows, prodName, prodPrice, prodCategories, prodState, prodRemark);
        List<BaseDict> categoriesType = baseDictService.findBaseDictByTypeCode(CATEGORIES_TYPE);
        model.addAttribute("page", productsList);
        model.addAttribute("prodName", prodName);
        model.addAttribute("prodPrice", prodPrice);
        model.addAttribute("prodCategories", prodCategories);
        model.addAttribute("prodState", prodState);
        model.addAttribute("prodRemark", prodRemark);
        model.addAttribute("categoriesType", categoriesType);
//        System.out.println(productsList.getRows().get(0));
        return "products";
    }
    
    //产品创建
    @RequestMapping("/create.action")
    @ResponseBody
    public String productsCreate(Products products) {

        int rows = productsService.createProducts(products);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
    
    //通过id查询产品
    @RequestMapping("/getProductsById.action")
    @ResponseBody
    public Products getProductsById(Integer id) {
        return productsService.getProductsById(id);

    }
    
    //更新产品
    @RequestMapping("/update.action")
    @ResponseBody
    public String productsUpdate(Products products) {
        int rows = productsService.updateProducts(products);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
    
    //删除产品
    @RequestMapping("/delete.action")
    @ResponseBody
    public String productsDelete(Integer id) {
        int rows = productsService.deleteProducts(id);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
    

}